package com.jt.demos;

import redis.clients.jedis.Jedis;

import java.util.UUID;
/*
通过redis实现单点登录
 */
public class SSOdemo1 {
    static String token;

    public static void main(String[] args) {
        //1.
        doGetResource();

        doLogin("jack","123456");

        doGetResource();
    }

    private static void doLogin(String username,String password) {
        if ("jack".equals(username)&&"123456".equals(password)){
            System.out.println("login ok");
            String token = UUID.randomUUID().toString();

            Jedis jedis = new Jedis("192.168.126.128", 6379);
            jedis.set(token, username);
            jedis.close();
            SSOdemo1.token=token;
            return;
        }
        System.out.println("username or password error");
    }

    //1.模仿客户端访问服务端资源
    private static void doGetResource() {
        if (token == null) {
            System.out.println("请登录");
            return;
        }
        Jedis jedis = new Jedis("192.168.126.128",6379);//连接redis
        String user = jedis.get(token);//从redis里面取
        jedis.close();
        if (user==null){
            System.out.println("login timeout");
            return;
        }
        System.out.println("return your resource");
    }

}
